import re

from django.contrib.auth.backends import ModelBackend

from users.models import User


def jwt_response_payload_handler(token, user=None, request=None):
    """
    自定义jwt认证成功返回数据
    """
    return {
        'token': token,
        'user_id': user.id,
        'username': user.username
    }

def get_user_by_account(account):
    try:
        if re.match(r'1[345789]\d{9}',account):
            user = User.objects.get(mobile=account)
        else:
            user = User.objects.get(username=account)
    except user.DoesNotExist as e:
        user = None

    return user

class UsernameMobileAuthBackend(ModelBackend):
    def authenticate(self, request, username=None, password=None, **kwargs):
        # try:
        #     if re.match(r'1[345789]\d{9}',username):
        #     #     手机号
        #         user = User.objects.get(mobile=username)
        #     else:
        #     #     用户名
        #         user = User.objects.get(username=username)
        # except user.DoesNotExist as e:
        #     return None
        user = get_user_by_account(username)
        if user is not None and user.check_password(password):
            return user



